<cfif Not IsDefined("session.id")>
		<cflocation url = "/ecada/" addToken = "no">
</cfif>
	
<cfif IsDefined("session.experiment")>
	<cfset experiment = session.experiment>
<cfelse>
	<cflocation url="/ecada/">
</cfif>

<cfif IsDefined("form.submit")>
	
	<cfswitch expression="#Atypes#">
		<cfcase value="likert">
			<!---<cfquery datasource="exp">
				INSERT INTO question(question, typeID, QuestionInfoID, StimID)
				VALUES('#Question#', 1, -1, #StimuliID#)
			</cfquery>--->
		</cfcase>
		
		<cfcase value="text">
		
			<cfquery datasource="exp">
				INSERT INTO question(question, typeID, QuestionInfoID, expid)
				VALUES('#Question#', 2, -1, #experiment.id#)
			</cfquery>
			
		</cfcase>
		
		<cfcase value="radio">
		
			<!---Create Radio Group--->
			<cfquery datasource="exp" result="radGrp">
				INSERT INTO radiogroup(Title)
				VALUES('AAAAAAAAAA')
			</cfquery>
			
			<!---loop over all radio button entries ignoring any that have been deleted--->
			<cfloop from="1" to="#rbcount#" index="i">
				<cfif IsDefined("form.rb" & i)>
					<cfquery datasource="exp">
						INSERT INTO radio(text, radioGroupID)
						VALUES('#form['rb' & i]#', #radGrp.GENERATED_KEY#)
					</cfquery>
				</cfif>
			</cfloop>
			
			<!---insert question into database--->
			<cfquery datasource="exp">
				INSERT INTO question(question, typeID, QuestionInfoID, expid)
				VALUES('#Question#', 3, #radGrp.GENERATED_KEY#, #experiment.id#)
			</cfquery>
			
		</cfcase>
		
		<cfcase value="checkbox">
		
			<!---Create CheckboxGroup--->
			<cfquery datasource="exp" result="CBGrp">
				INSERT INTO CheckboxGroup(Title)
				VALUES('AAAAAAAAAA')
			</cfquery>
			
			<!---loop over all radio button entries ignoring any that have been deleted--->
			<cfloop from="1" to="#cbcount#" index="i">
				<cfif IsDefined("form.cb" & i)>
					<cfquery datasource="exp">
						INSERT INTO Checkbox(Text, CheckboxGroupID)
						VALUES('#form['cb' & i]#', #CBGrp.GENERATED_KEY#)
					</cfquery>
				</cfif>
			</cfloop>
			
			<!---insert question into database--->
			<cfquery datasource="exp">
				INSERT INTO question(question, typeID, QuestionInfoID, expid)
				VALUES('#Question#', 4,  #CBGrp.GENERATED_KEY#, #experiment.id#)
			</cfquery>
			
		</cfcase>
	</cfswitch>

	<cflocation url="./../" addToken="no">
<cfelse>
	<cfinclude template= "/ecada/header.cfm">
	<cfoutput>
		<form method="post" action="./">
					<fieldset>
			<legend>
				Question
			</legend>
			
			<table>
				<tr>
					<td>
						Question
					</td>
					<td>
						<input type="textbox" name="Question" id="Question">
					</td>
				</tr>
				<tr>
					<td>	
						Numerical answer?
					</td>
					<td>
						<input type="checkbox" value="0" name="Numeric" id="Numeric"><br>
					</td>
				</tr>
				<tr>
					<td>	
						Max Question Time
					</td>
					<td>
						<input type="textbox" value="0" disabled="true" name="MaxQ" id="MaxQ">(disabled)
						<br>
					</td>
				</tr>
			</table>
		</fieldset>
		<fieldset>
				<legend>
					Answer Type
				</legend>
				<select name="Atypes" id="Atypes">
					<cfquery datasource="exp" name="AnswerType">
						SELECT id,type
						FROM qestiontype
						ORDER BY type ASC
					</cfquery>
					<cfset first = 1>
					<cfloop query="AnswerType">
						<cfif first eq 1>
							<option value="#HTMLEditFormat(type)#" selected="true">#HTMLEditFormat(type)#</option>
							<cfset first = 0>
						<cfelse>
							<option value="#HTMLEditFormat(type)#">#HTMLEditFormat(type)#</option>
						</cfif>
					</cfloop>
				</select>
				<script type="text/javascript">
					$(document).ready(function() {
						var sel =  $('##Atypes').val();
						$('##Answer div').hide();
						$('.Answer-' + sel).show();
						$('##TrialAnswerType').val(sel);
						redrawShadows();
					});

					$('##Atypes').change(function(){
						var sel =  $('##Atypes').val();
						$('##Answer div').hide();
						$('.Answer-' + sel).show();
						$('##TrialAnswerType').val(sel);
						redrawShadows();
					});
				</script>
				<div name="Answer" id="Answer">
					<cfinclude template="Answer/text.cfm">
					<cfinclude template="Answer/radio.cfm">
					<cfinclude template="Answer/likert.cfm">
					<cfinclude template="Answer/checkbox.cfm">
				</div>
			</fieldset>
			<input type="submit" name="submit" value="Submit"></input>
		</form>
	</cfoutput>
	<cfinclude template= "/ecada/footer.cfm">
</cfif>



